package com.dw.wms.basedata.controller;

import com.dw.wms.basedata.domain.Company;
import com.dw.wms.basedata.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basedata/company")
@RequiredArgsConstructor
@Api(tags = "公司管理")
public class CompanyController {

    private final CompanyService companyService;

    /**
     * 获取所有公司列表
     */
    @GetMapping
    @ApiOperation("获取公司列表")
    public ResponseEntity<List<Company>> getAllCompanies() {
        List<Company> companies = companyService.list();
        return ResponseEntity.ok(companies);
    }

    /**
     * 根据ID获取公司详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取公司详情")
    public ResponseEntity<Company> getCompanyById(@PathVariable Long id) {
        Company company = companyService.getById(id);
        if (company != null) {
            return ResponseEntity.ok(company);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新公司
     */
    @PostMapping
    @ApiOperation("创建公司")
    public ResponseEntity<Company> createCompany(@RequestBody Company company) {
        // 检查必要字段是否为空
        if (company.getCompanyCode() == null || company.getCompanyName() == null) {
            return ResponseEntity.badRequest().build();
        }
        
        // 设置默认状态为启用(0)
        if (company.getStatus() == null) {
            company.setStatus("0");
        }
        
        // 执行保存操作
        boolean saved = companyService.save(company);
        if (saved) {
            // 重新查询以获取完整信息（包括自动生成的ID等）
            Company savedCompany = companyService.getById(company.getId());
            return ResponseEntity.status(HttpStatus.CREATED).body(savedCompany);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 更新公司信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新公司信息")
    public ResponseEntity<Company> updateCompany(@PathVariable Long id, @RequestBody Company company) {
        // 检查要更新的公司是否存在
        Company existingCompany = companyService.getById(id);
        if (existingCompany == null) {
            return ResponseEntity.notFound().build();
        }

        // 设置ID以确保更新而不是插入
        company.setId(id);

        // 执行更新操作
        boolean updated = companyService.updateById(company);
        if (updated) {
            // 返回更新后的完整对象
            Company updatedCompany = companyService.getById(id);
            return ResponseEntity.ok(updatedCompany);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }


    /**
     * 删除公司
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除公司")
    public ResponseEntity<Void> deleteCompany(@PathVariable Long id) {
        boolean deleted = companyService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
